package com.ld.security.common.mybatis.core.handlers;

import com.ld.security.common.core.base.column.ListString;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MappedJdbcTypes({JdbcType.VARCHAR})
public class ListStringTypeHandler extends BaseTypeHandler<ListString> {


    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, ListString parameter, JdbcType jdbcType) throws SQLException {
        String val = parameter.getValue();
        ps.setString(i,val);
    }

    @Override
    public ListString getNullableResult(ResultSet rs, String columnName) throws SQLException {

        String val = rs.getString(columnName);
        ListString listString ;
        if(val == null){
            listString =  new ListString();
        }else{
            listString =  new ListString(val);
        }

        return listString;
    }

    @Override
    public ListString getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String val = rs.getString(columnIndex);
        ListString listString ;
        if(val == null){
            listString =  new ListString();
        }else{
            listString =  new ListString(val);
        }


        return listString;
    }

    @Override
    public ListString getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String val = cs.getString(columnIndex);
        ListString listString ;
        if(val == null){
            listString =  new ListString(val);
        }else{
            listString =  new ListString(val);
        }


        return listString;
    }

}
